package com.qinggeng.project.api.controller;

import com.qinggeng.common.constant.Constants;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.dto.UserSearchDto;
import com.qinggeng.project.api.security.annotation.LoginRequired;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.service.IUserSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.qinggeng.project.api.security.utils.ApiUtils.getIsLogin;

@Api(tags = "搜索记录模块")
@RestController
@RequestMapping("${apiPath}/search")
public class ApiSearchController extends BaseController {

    @Autowired
    private IUserSearchService searchService;

    @ApiOperation("查询用户搜索记录")
    @GetMapping("/list")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记", required = false),
    })
    @LoginRequired(must = false,role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult selectSearchList(){
        List<UserSearchDto> userSearchDtoList=new ArrayList<>();
        if(getIsLogin()){
            userSearchDtoList=searchService.selectUserSearchListDtoByUserId(ApiUtils.getUserId());
        }
        return AjaxResult.success(userSearchDtoList);
    }

    @ApiOperation("清除用户搜索记录")
    @PostMapping("/clear")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", dataType = "String", name = Constants.TOKEN, value = "token标记", required = false),
    })
    @LoginRequired(must = false,role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult clearSearchList(){
        int rows=0;
        if(getIsLogin()){
            rows=searchService.deleteSearchByUserId(ApiUtils.getUserId());
        }
        return AjaxResult.success(rows);
    }


}
